package com.lxk.controller;

import com.lxk.service.OrdersService;
import com.lxk.service.RefundService;
import com.lxk.vo.PageModel;
import com.lxk.vo.RefundDetailModel;
import com.lxk.vo.RefundModel;
import com.lxk.vo.result.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author JohnSoybean
 * @create 2020/3/31 17:54
 * @description
 */
@Controller
public class RefundController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrdersService os;
    @Autowired
    private RefundService rs;

    /**
     * 初始化退款请求列表页面
     * @param req
     * @param model
     * @param pageIndex
     * @param num
     * @param totalPages
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/refund")
    public String toRefund(HttpServletRequest req, Model model,
                         Integer pageIndex, Integer num, Integer totalPages,
                         Integer sort, Integer order, Integer refundStatus) {
        if (null == pageIndex) {
            pageIndex = 1;
        }
        if (null == num) {
            num = 12;
        }
        if (null == totalPages) {
            totalPages = rs.getTotalPages(num, refundStatus);
        }
        PageModel<RefundModel> refundInfo = rs.selRefundPage(pageIndex, num, totalPages, sort, order, refundStatus);
        model.addAttribute("data", refundInfo);
        return "refund";
    }


    /**
     * 拒绝退款
     * @param req
     * @param refundId
     * @return
     */
    @GetMapping("/refund/reject")
    @ResponseBody
    public Result<Integer> rejectRefund(HttpServletRequest req, Integer refundId) {
        rs.rejectRefund(refundId);
        return Result.success(1);
    }


    /**
     * 获取退款详情信息
     * @param req
     * @param refundId
     * @return
     */
    @GetMapping("/refund/detail")
    @ResponseBody
    public Result<RefundDetailModel> getRefundDetail(HttpServletRequest req, Integer refundId) {
        RefundDetailModel model = rs.getRefundDetail(refundId);
        return Result.success(model);
    }

}
